<?php

namespace app\common\model;

use basic\ModelBasic;

class PinkConfig extends ModelBasic
{
	use \traits\ModelTrait;

    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*")
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();

        return static::process_list([$item])[0];
    }

    public static function process_list (array $list)
    {
        return $list;
    }

	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);
        if ( isset($where['status']) && $where['status'] != '' ) {
            $model->where(sprintf('%s.status=%d' , $alias , $where['status']));
        }

        if ( isset($where['rel_type']) && $where['rel_type'] != '' ) {
            $model->where(sprintf("%s.rel_type='%s'" , $alias , $where['rel_type']));
        }

        if ( isset($where['keyword']) && $where['keyword'] != '' ) {
            $model->where("$alias.rel_name" , 'LIKE' , "%".$where['keyword']."%");
        }

        if ( static::$softDelete ) {
            $model->where($alias.'.is_del',0);
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }
}//Class End
